<?php
class TasksController extends AppController {
    var $name = 'Tasks';
    
    function add() {
        $this->pageTitle = "添加任务";
        if ($this->Auth->user('role') == 'teacher') {
            if ($this->data) {
                if (mkdir('homeworks/'.$this->data['Task']['upload_folder'])) {
                    $this->Task->create();
                    $this->Task->save($this->data);
                    $this->Task->save(array('start_time' => date( 'Y-m-d H:i:s')));
                    $this->Session->setFlash("任务添加成功");
                    $this->redirect(array('controller' => 'teacher_profiles', 'action' => 'index'));
                }
                else {
                    $this->Session->setFlash("无法创建目录请重试");
                }
            }
        }
        else {
            $this->Session->setFlash("您没有相应的权限");
            $this->redirect(array('controller' => 'users', 'action' => 'login'));
        }
    }
    
    function edit($id = null) {
        $this->pageTitle = "编辑任务信息";
        if ($this->Auth->user('role') == 'teacher') {
            $this->Task->id = $id;
            if (!$this->data) {
                $this->data = $this->Task->read();
            }
            else {
                $this->Task->save($this->data);
                $this->Session->setFlash("任务修改成功");
            }
        }
        else {
            $this->Session->setFlash("您没有相应的权限");
            $this->redirect(array('controller' => 'users', 'action' => 'login'));
        }
    }
    
    function close($id = null) {
        $this->pageTitle = "关闭任务";
        $this->Task->id = $id;
        if ($this->Auth->user('role') == 'teacher') {
            $this->Task->save(array('closed' => True, 'end_time' => date('Y-m-d H:i:s')));
        }
    }
    
    function available() {
        $this->pageTitle = "开放中的任务";
        if ($this->Auth->user('role') == 'student') {
            $this->set('data', $this->Task->findAllByClosed(False));
        }
        else {
            $this->Session->setFlash("您没有相应的权限");
            $this->redirect(array('controller' => 'users', 'action' => 'login'));
        }
    }
    
    function all() {
        $this->pageTitle = "全部任务";
        if ($this->Auth->user('role') == 'teacher') {
            $this->set('data', $this->Task->find('all'));
        }
        else {
            $this->Session->setFlash("您没有相应的权限");
            $this->redirect(array('controller' => 'users', 'action' => 'login'));
        }
    }
    
    function choose_for_evaluate() {
        $this->pageTitle = "选择任务以添加作业评价";
        $available_tasks = $this->Task->findAllByClosed(False);
        $arr_for_options = array();
        foreach ($available_tasks as $single_task):
            $arr_for_options[$single_task['Task']['id']] = $single_task['Task']['task_name'];
        endforeach;
        $this->set('ops', $arr_for_options);
        if ($this->Auth->user('role') == 'teacher') {
            if ($this->data) {
                $this->redirect(array('controller' => 'homeworks', 'action' => 'list_for_evaluate', $this->data['Task']['task_id']));
            }
        }
        else {
            $this->Session->setFlash("您没有相应的权限");
            $this->redirect(array('controller' => 'users', 'action' => 'login'));
        }
    }
}
?>